package com.cndcare.msc.controller;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.cndcare.msc.bean.PcBean;
import com.cndcare.msc.handler.ClientChannel;
import com.cndcare.msc.protobuf.CmdEnum;
import com.cndcare.msc.protobuf.MessageVo;
import com.cndcare.msc.util.IPUtils;
import com.cndcare.msc.util.LocalMac;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
public class IndexController {

    @Value("${server.domain}")
    private String serverDomain;

    private String pcInfoUrl = "/api/pc/pcInfo";
    private String saveAndFlushPc = "/api/pc/savePc";
    @Autowired
    private ObjectMapper objectMapper;

    @RequestMapping("/pcInfo")
    public String pcInfo(Map<String, Object> map) throws IOException {

        String mac = LocalMac.getLocalMac();

        String str = HttpRequest.get(serverDomain + pcInfoUrl)
                .timeout(5000)
                .form("mac", mac)
                .form("ip", IPUtils.getRealIp())
                .execute().body();
        PcBean pcBean = objectMapper.readValue(str, PcBean.class);

        map.put("pcBean", pcBean);

        return "pc_info";
    }

    @RequestMapping("/savePc")
    @ResponseBody
    public Map<String, Object> savePc(PcBean pcBean) {
        Map<String, Object> jsonMap = new HashMap<>();

        try {
            String mac = LocalMac.getLocalMac();

            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("mac", mac);
            paramMap.put("address", pcBean.getAddress());
            paramMap.put("remark", pcBean.getRemark());
            HttpResponse execute = HttpRequest.post(serverDomain + saveAndFlushPc)
                    .timeout(5000)
                    .form(paramMap)
                    .execute();
            if (execute.getStatus() != 200) {
                throw new RuntimeException("服务器异常，网络不通或未启动");
            }
            String str = execute.body();
            PcBean tmpPc = objectMapper.readValue(str, PcBean.class);

            if (null != ClientChannel.ctx) {
                ClientChannel.ctx.writeAndFlush(
                        MessageVo.builder()
                                .mac(tmpPc.getMac())
                                .ip(IPUtils.getRealIp())
                                .cmdEnum(CmdEnum.REFRESH_ALL)
                                .data(tmpPc.getMac() + "更新了数据")
                                .build()
                );
            }

            jsonMap.put("flag", true);
        } catch (Exception e) {
            e.printStackTrace();
            jsonMap.put("flag", false);
            jsonMap.put("errorMsg", e.getMessage());
        }

        return jsonMap;
    }

}
